package ssm.another;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by libing on 2016/12/13.
 */
public class CheckUtil {
    //判断是否为邮箱格式
    public static boolean checkEmailFormat(String email) throws EmptyException {
        if (email == null){
            throw new EmptyException("Email Empty");
        }
        String RULE_EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
        //正则表达式的模式
        Pattern p = Pattern.compile(RULE_EMAIL);
        //正则表达式的匹配器
        Matcher m = p.matcher(email);
        //进行正则匹配
        return m.matches();

    }

    //判断是否为手机格式
    public static boolean checkMobileFormat(String phone) throws Exception {
        if (phone == null) {
            throw new EmptyException("Phone Empty");
        }
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(phone);
        return m.matches();
    }

    //判断密码长度是否在6位到15位之间
    public static boolean checkPasswordLength(String password) throws EmptyException {
        if (password == null){
            throw new EmptyException("Password Empty");
        }
        if(password.length()>=6 && password.length() <= 15)
            return true;
        return false;
    }


    //判断密码是否含有字母、数字以及密码长度是否在6-15位之间
    public static boolean checkPasswordFormat(String password) throws EmptyException {
        if (password == null){
            throw new EmptyException("Password Empty");
        }
        final String regx = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$";
        /*
        * 分开来注释一下：
^        匹配一行的开头位置
        (?![0-9]+$) 预测该位置后面不全是数字
        (?![a-zA-Z]+$) 预测该位置后面不全是字母
        [0-9A-Za-z] {8,16} 由8-16位数字或这字母组成
        $ 匹配行结尾位置
        注：(?!xxxx) 是正则表达式的负向零宽断言一种形式，标识预该位置后不是xxxx字符。
        * */
        final Pattern pattern = Pattern.compile(regx);
        final Matcher matcher = pattern.matcher(password);
        return matcher.find();
    }
}
